﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class TahasukaWorkingRequestCard : UserControl
    {
        public static readonly string TableName = "TahasukaRequests";
        TahasukaCard tCard;
        DataTable dt = new DataTable();
        SqlDataAdapter da;

        public SqlDataAdapter SqlDataAdapter
        {
            get  { return da; }
            set  {  da = value; }
        }

        //Ctor
        public TahasukaWorkingRequestCard(TahasukaCard card)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            tCard = card;

            string selectStr = CompanyManager.getSqlStringFromFile(@"\SELECT\TahasukaRequests.SELECT.sql");
            string updateStr = CompanyManager.getSqlStringFromFile(@"\UPDATE\TahasukaRequests.UPDATE.sql");
            string deleteStr = "DELETE FROM [TahasukaRequests] WHERE (requestID=@requestID)";
            string insertStr = "INSERT INTO [TahasukaRequests] VALUES (@armyID,@startDate,@endDate,@comments)";

            da = SqlManager.SqlDataAdapterFactory(selectStr);
            da.UpdateCommand = SqlManager.SqlCommandFactory(updateStr);
            da.DeleteCommand = SqlManager.SqlCommandFactory(deleteStr);
            da.InsertCommand = SqlManager.SqlCommandFactory(insertStr);

            da.SelectCommand.Parameters.Add("@startDate", SqlDbType.Date).Value = tCard.StartDate;
            da.SelectCommand.Parameters.Add("@endDate", SqlDbType.Date).Value = tCard.EndDate;

            ICollection<string> sqlParameterCollection = CompanyManager.parseSqlQueryParameters(updateStr);
            sqlParameterCollection.Distinct<string>();
            sqlParameterCollection.Add("armyID"); //thats because it ain't contained in the update string!!!

            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
            {
                string sItem = "@" + item;
                switch (item)
                {
                    case "armyID":   
                        da.InsertCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));
                        break;

                    case "requestID":
                        da.UpdateCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.Int, DataRowVersion.Current, item));
                        da.DeleteCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.Int, DataRowVersion.Current, item));    
                        break;
    
                    case "comments": 
                        da.UpdateCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Current, "comments")); 
                        da.InsertCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Current, "comments"));   
                        break;

                    default:        
                        DateTime dt1 = tCard.StartDate;

                        da.UpdateCommand.Parameters.Add(new SqlParameter("@" + item, SqlDbType.Date));        
                        da.InsertCommand.Parameters.Add(new SqlParameter("@" + item, SqlDbType.Date));
        
                        da.UpdateCommand.Parameters["@" + item].SourceColumn = item;            
                        da.InsertCommand.Parameters["@" + item].SourceColumn = item;

                                   
                        da.UpdateCommand.Parameters["@" + item].SourceVersion = DataRowVersion.Current;          
                        da.InsertCommand.Parameters["@" + item].SourceVersion = DataRowVersion.Current;
                                  
                        break; 
                }
            }

            //Populate the DataSet by running the Fill method of the SqlDataAdapter.
            da.Fill(dt);
            dataGridView.DataSource = dt;
            CompanyManager.setTableColumnHeadersHebrew(dataGridView.Columns);
            refresh();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                da.Update(dt);
                labelSuccessfulUpdate.Visible = true;
                labelSuccessfulUpdate.Location = new Point(splitContainer.Panel1.Bounds.Width / 2, 16);
                timer1.Interval = 5 * 1000;
                timer1.Start();
                refresh();
            }
            catch (Exception)
            {
                
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            BaseCollection rows = dataGridView.SelectedRows;
                       
            int armyID_index = dataGridView.Columns["armyID"].Index;
            tCard.deleteRequests(rows, armyID_index);
            da.Update(dt);

            refresh();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            ZimunimList z_list = new ZimunimList(tCard, null, ZimunimList.TableOption.AlreadyCalled);
            dataGridView.Columns["comments"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            z_list.Text = "רשימת לוחמים שזומנו לתעסוקה";
            z_list.ShowDialog();

            tCard.addSoldierRequest();
            refresh();
        }

        private void refresh()
        {
            tCard.ArmyIDList.Clear();
            dt.Clear();
            da.Fill(dt);
            dataGridView.DataSource = dt;
            

            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;
            dataGridView.Columns["comments"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView.Columns["requestID"].Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }

       
    }
}
